package com.yycwxt.modules.admin.company;

import com.yycwxt.common.ReturnResult;
import com.yycwxt.domain.Company;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DuplicateKeyException;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

@Api
@RestController
@Validated
@RequestMapping("/company")
public class CompanyController{

    private final CompanyService companyService;

    @Autowired
    public CompanyController( CompanyService companyService){
        this.companyService = companyService;
    }

    @PutMapping()
    @ApiOperation("保存公司信息")
    public ReturnResult save(@Validated @RequestBody @ApiParam(name="company",required=true,type="company")Company company){
        try {
            if (companyService.updateById(company)) {
                return ReturnResult.buildSuccessResult("保存成功");
            } else {
                return ReturnResult.buildFailureResult("id可能不存在或者name重复", false);
            }
        } catch (DuplicateKeyException dke) {
            return ReturnResult.buildFailureResult("name 值可能重复", false);
        } catch (Exception e) {
            return ReturnResult.buildFailureResult("参数错误");
        }
    }

    @GetMapping()
    @ApiOperation("查询公司信息")
        public ReturnResult find(){
            return ReturnResult.buildSuccessResult(companyService.getById(1));
        }

}